package com.tanhua.dubbo.api;

import com.tanhua.dubbo.utils.IdWorker;
import com.tanhua.model.enums.CommentType;
import com.tanhua.model.mongo.Video;
import com.tanhua.model.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

@DubboService
public class VideoApiImpl implements VideoApi {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Autowired
    private IdWorker idWorker;

    @Override
    public String save(Video video) {
        //1、设置属性
        video.setVid(idWorker.getNextId("video"));
        video.setCreated(System.currentTimeMillis());
        //2、调用方法保存对象
        mongoTemplate.save(video);
        //3、返回对象id
        return video.getId().toHexString();
    }

    @Override
    public List<Video> findMovementsByVids(List<Long> vids) {
        Query query = Query.query(Criteria.where("vid").in(vids));
        return mongoTemplate.find(query, Video.class);
    }

    @Override
    public List<Video> queryVideoList(int page, Integer pagesize) {
        Query query = new Query().limit(pagesize).skip((page - 1) * pagesize)
                .with(Sort.by(Sort.Order.desc("created")));
        return mongoTemplate.find(query, Video.class);
    }

    @Override
    public PageResult findByUserId(Integer page, Integer pagesize, Long userId) {
        Query query = Query.query(Criteria.where("userId").in(userId));
        long count = mongoTemplate.count(query, Video.class);
        query.limit(pagesize).skip((page - 1) * pagesize)
                .with(Sort.by(Sort.Order.desc("created")));
        List<Video> list = mongoTemplate.find(query, Video.class);
        return new PageResult(page, pagesize, count, list);
    }

    /**
     * 小视频点赞 or 评论 or 喜欢
     *
     * @param id
     */
    @Override
    public void updateLikeCount(String id, CommentType like) {
        Query query = Query.query(Criteria.where("id").is(new ObjectId(id)));
        Update update = new Update();
        if (like.getType() == 1) {
            //点赞
            update.inc("likeCount",1);
        }else if(like.getType() == 2){
            //评论
            update.inc("commentCount",1);
        }else{
            //喜欢
            update.inc("loveCount",1);
        }
        //更新到数据库
        mongoTemplate.updateFirst(query,update,Video.class);
    }

    @Override
    public void updateDisCount(String id, CommentType like) {
        Query query = Query.query(Criteria.where("id").is(new ObjectId(id)));
        Update update = new Update();
        if (like.getType() == 1) {
            //点赞
            update.inc("likeCount",-1);
        }else if(like.getType() == 2){
            //评论
            update.inc("commentCount",-1);
        }else{
            //喜欢
            update.inc("loveCount",-1);
        }
        //更新到数据库
        mongoTemplate.updateFirst(query,update,Video.class);
    }

    @Override
    public Long selectUserId(String id) {
        Query query = Query.query(Criteria.where("id").is(new ObjectId(id)));
        Video one = mongoTemplate.findOne(query, Video.class);

        return one.getUserId();
    }
}
